package com.gs.core.jms;

import java.util.concurrent.atomic.AtomicInteger;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.springframework.beans.factory.annotation.Autowired;

/**
	The JmsMessageListener implements the JMS interface MessageListener. 
	The int property for the message count can be retrieved before casting the message to TextMessage. 
	Then the message and message count are both logged.  
 */
public class JmsMessageListener implements MessageListener{
	 	@Autowired
	    private AtomicInteger counter = null;

	    /**
	     * Implementation of <code>MessageListener</code>.
	     */
	    public void onMessage(Message message) {
	        try {   
	            int messageCount = message.getIntProperty(JmsMessageProducer.MESSAGE_COUNT);
	            
	            if (message instanceof TextMessage) {
	                TextMessage tm = (TextMessage)message;
	                String msg = tm.getText();
	                
	                
	                System.out.println("Processed message '{"+msg+"}'.  value={"+messageCount+"}");
	                
	                counter.incrementAndGet();
	            }
	        } catch (JMSException e) {
	            System.out.println(e.getMessage());
	        }
	    }
}
